define(
   [
      'dojo/_base/declare',
      'dojo/_base/lang',
      'dojo/dom-style',
      'dojo/dom-construct',
      'dojo/Evented',
      'dijit/_WidgetBase',
      'dijit/_TemplatedMixin',
      'dijit/form/Button',
      'dojox/form/BusyButton',
      'dojo/text!./templates/buttonRow.html'
   ],
   function(declare, lang, domStyle, domConstruct, evented, _WidgetBase, _TemplatedMixin, Button, BusyButton, template)
   {
      return declare([_WidgetBase, _TemplatedMixin, evented],
      {
         //Path to the template
         templateString: template,
      
         widgetsInTemplate: true,
         baseClass: 'NoBusinessButtonRow',
      
         mBack: true,
         mBackLabel: "Back",
         mBackIconClass: "icons-fugue-icons-16x16-arrow-180",
         mCancel: true,
         mCancelLabel: "Cancel",
         mCancelIconClass: "icons-fugue-icons-16x16-cross-script",
         mNext: true,
         mNextLabel: "Next",
         mNextIconClass: "icons-fugue-icons-16x16-arrow",
         mSubmit: true,
         mSubmitBusyLabel: "Submitting...",
         mSubmitIconClass: "icons-diagona-icons-16x16-102",
         mSubmitLabel: "Submit",
         mApply: false,
         mApplyLabel: "Apply",
         mApplyBusyLabel: "Applying...",
         mApplyIconClass: "icons-fugue-icons-16x16-disk-black",
         postscript: function(args)
         {
            this.inherited(arguments);
         },
         postCreate: function()
         {
            // Run any parent postCreate processes - can be done at any point
            this.inherited(arguments);
            this.mCustomButtons = [];
            
            if(this.mBack)
            {
               this.mBackButton = new Button({
                  label: this.mBackLabel,
                  iconClass: 'dijitIcon ' + this.mBackIconClass,
                  onClick: lang.hitch(this, function(){
                     this.emit('back');
                  })
               }, this.mAttachPointForNoBusinessBackButton);
            }
            
            if(this.mNext)
            {
               this.mNextButton = new Button({
                  label: this.mNextLabel,
                  iconClass: 'dijitIcon ' + this.mNextIconClass,
                  onClick: lang.hitch(this, function(){
                     this.emit('next');
                  })
               }, this.mAttachPointForNoBusinessNextButton);
            }
            
            if(this.mSubmit)
            {
               this.mSubmitButton = new BusyButton({
                  label: this.mSubmitLabel,
                  type: 'submit',
                  busyLabel: this.mSubmitBusyLabel,
                  iconClass: 'dijitIcon ' + this.mSubmitIconClass,
                  onClick: lang.hitch(this, function(evt) {
                     evt.preventDefault();
                     this.disableButtons(this.mSubmitButton);
                     this.emit('submit', {close: true});
                  })
               }, this.mAttachPointForNoBusinessSubmitButton);
            }

            if(this.mApply)
            {
               this.mApplyButton = new BusyButton({
                  label: this.mApplyLabel,
                  busyLabel: this.mApplyBusyLabel,
                  iconClass: 'dijitIcon ' + this.mApplyIconClass,
                  onClick: lang.hitch(this, function() {
                     this.disableButtons(this.mApplyButton);
                     this.emit('apply');
                  })
               }, this.mAttachPointForNoBusinessApplyButton);
            }
            
            if(this.mCancel)
            {
               this.mCancelButton = new Button({
                  label: this.mCancelLabel,
                  iconClass: 'dijitIcon ' + this.mCancelIconClass,
                  onClick: lang.hitch(this, function() {
                     this.emit('cancel');
                  })
               }, this.mAttachPointForNoBusinessCancelButton);
            }
         },
         disableButtons: function(pButtonToSkip)
         {
            if(this.mSubmitButton)
               this.mSubmitButton.set('disabled', true);
            if(this.mApplyButton)
               this.mApplyButton.set('disabled', true);
            if(this.mCancelButton)
               this.mCancelButton.set('disabled', true);
            if(this.mNext)
               this.mNextButton.set('disabled', true);
            if(this.mBack)
               this.mBackButton.set('disabled', true);
            for(var idx = 0; idx < this.mCustomButtons.length; idx++)
            {
               this.mCustomButtons[idx].set('disabled', true);
            }
         },
         addCustomButton: function(pButton)
         {
            this.mCustomButtons.push(pButton);
            domConstruct.place(pButton.domNode, this.mPlaceHolderForCustomButtons, "after");
         },
         removeCustomButton: function(pButton)
         {
            if(this.mCustomButtons.indexOf(pButton) < 0)
               return false;
            
            this.mCustomButtons = this.mCustomButtons.slice(this.mCustomButtons.indexOf(pButton), 1);
            return true;
         },
         reset: function()
         {
            setTimeout(lang.hitch(this, this.doReset), 10);
         },
         doReset: function()
         {
            if(this._destroyed)
               return;

            if(this.mSubmitButton)
            {
               this.mSubmitButton.cancel();
               this.mSubmitButton.set('disabled', false);
            }
            
            if(this.mApplyButton)
            {
               this.mApplyButton.cancel();
               this.mApplyButton.set('disabled', false);
            }
            
            if(this.mCancelButton)
               this.mCancelButton.set('disabled', false);
            
            if(this.mNext)
               this.mNextButton.set('disabled', false);

            if(this.mBack)
               this.mBackButton.set('disabled', false);

            for(var idx = 0; idx < this.mCustomButtons.length; idx++)
            {
               if(this.mCustomButtons[idx].cancel)
                  this.mCustomButtons[idx].cancel();
               this.mCustomButtons[idx].set('disabled', false);
            }
         },
         hide: function()
         {
            if(this.mSubmitButton)
               domStyle.set(this.mSubmitButton.domNode, "display", "none");
            if(this.mApplyButton)
               domStyle.set(this.mApplyButton.domNode, "display", "none");
            if(this.mCancelButton)
               domStyle.set(this.mCancelButton.domNode, "display", "none");
            if(this.mNextButton)
               domStyle.set(this.mNextButton.domNode, "display", "none");
            if(this.mBackButton)
               domStyle.set(this.mBackButton.domNode, "display", "none");
            if(this.mResetButton)
               domStyle.set(this.mResetButton.domNode, "display", "none");
            
            for(var idx = 0; idx < this.mCustomButtons.length; idx++)
            {
               domStyle.set(this.mCustomButtons[idx].domNode, "display", "none");
            }
         },
         submitHidden: function(hidden)
         {
            domStyle.set(this.mSubmitButton.domNode, "visiblity", (hidden ? 'hidden' : 'visible'));
            domStyle.set(this.mSubmitButton.domNode, "display", (hidden ? 'none' : 'block'));
         }
      });
   }
);
